<?php
/**
 * funkcja do obsługi tabelek na froncie
 * przepisana funkcja smarty_function_tables
 */
function smarty_function_ftab($param, &$smarty) {
	
	if(!isset($param['tid'])) {
		throw new Exception('Musisz podać argument tid do pluginu smarty_function_ftab');
	}
	
	$tid = $param['tid'];
	
	$type = isset($param['type'])?$param['type']:null;
	$return = '';
	$table_data = $smarty->get_template_vars('table_data');
	$paramFromUrl = $smarty->get_template_vars('param_from_url');
	
	$not_in_use = array(
		'methodToRun','urlParam'
	);
	
	
	foreach($not_in_use AS $niu)if(isset($paramFromUrl[$niu]))unset($paramFromUrl[$niu]);
	$table_data = $table_data[$tid];
	
	if(!isset($paramFromUrl['page']))
	{
		$paramFromUrl['page'] = 1;
	}	
	
	if(!isset($paramFromUrl['t']))
	{
		$paramFromUrl['t'] = 10;
	}
	
	if(!isset($paramFromUrl['v']))
	{
		$paramFromUrl['v'] = 'a';
	}
	
	if(!isset($paramFromUrl['o']))
	{
		$paramFromUrl['o'] = '_';
	}	
	
	if(!isset($paramFromUrl['query']))
	{
		$paramFromUrl['query'] = '-';
	}
	
	switch($type) {
		case 'lp':
			$curr_page = isset($paramFromUrl['page'])?$paramFromUrl['page']:1;
			$iteration = isset($param['iteration'])?$param['iteration']:1;
			$total = $table_data['t'];
			
			$return = ($curr_page - 1) * $total + $iteration; 
			
			break;
		case 'on_page':
			
			$partial = isset($param['partial'])?$param['partial']:'partial/Shared/Tables/OnPage.tpl';
			$available = array(
				10, 20, 30, 50
			);
			$onP = array();
			foreach($available AS $count) {
				$onP[] = array(
					'number' => $count,
					'url' => Router::GenerateUrl((isset($param['urlLabel'])?$param['urlLabel']:'smarty_generated_'.microtime()),array_merge($paramFromUrl, array('t' => $count, 'page' => 1, 'v'=>'a', 'o'=>'_'))),
					'selected' => $count == $table_data['t']?'selected="selected"':''
				);
			}
			
			$smarty->assign('selected', isset($paramFromUrl['t'])?$paramFromUrl['t']:$available[0]);
			$smarty->assign('onP',$onP);
			$return = $smarty->fetch($partial);
			break;
		case 'pagination':
			// stronicowanie
			$partial = isset($param['partial'])?$param['partial']:'partial/Shared/Tables/Pagination.tpl';
			
			$page_count = ceil($param['all_count']/$table_data['t']);
			$curr_page = isset($paramFromUrl['page'])?$paramFromUrl['page']:1;
			
			$special = array();

			$pages = array();
			
			for($i = 1; $i <= $page_count; $i++)
			{
				
				if($i == 1)
				{
					$special['first'] = array(
						'number' => $i,
						'url' => Router::GenerateUrl((isset($param['urlLabel'])?$param['urlLabel']:'smarty_generated_'.microtime()),array_merge($paramFromUrl, array('page' => $i))),
						'selected' => ($i == $curr_page)?'class="active"':''
					);						
				}
				
				if($i == $page_count)
				{
					$special['last'] = array(
						'number' => $i,
						'url' => Router::GenerateUrl((isset($param['urlLabel'])?$param['urlLabel']:'smarty_generated_'.microtime()),array_merge($paramFromUrl, array('page' => $i))),
						'selected' => ($i == $curr_page)?'class="active"':''
					);						
				}				
				
				$_diff = 3;
				if(($curr_page - $_diff ) < 0)
				{
					$_diff += abs(($curr_page - $_diff ));
				}
				
				if(($curr_page + $_diff) > $page_count)
				{
					
					$_diff += abs($page_count - ($curr_page + $_diff ));
				}
				
				if($i > ($curr_page - $_diff) && $i < ($curr_page + $_diff))
				{
					$pages[$i] = array(
						'number' => $i,
						'url' => Router::GenerateUrl((isset($param['urlLabel'])?$param['urlLabel']:'smarty_generated_'.microtime()),array_merge($paramFromUrl, array('page' => $i))),
						'selected' => ($i == $curr_page)?'class="active"':''
					);					
				}
			}
			
			if(isset($pages[$curr_page - 1]))
			{
				$special['prev'] = $pages[$curr_page - 1];
			} else {
				$pages[1];
			}
			
			if(isset($pages[$curr_page + 1]))
			{
				$special['next'] = $pages[$curr_page + 1];
			} else {
				$pages[$curr_page];
			}			
			
			
			$smarty->assign('special', $special);
			$smarty->assign('pages',$pages);
			$smarty->assign('page_count', $page_count);
			$return = $smarty->fetch($partial);
			break;
		default:
			// nagłówki tabelki
			
			$field = isset($param['o'])?$param['o']:'_';
			$partial = isset($param['partial'])?$param['partial']:'partial/Shared/Tables/Header.tpl';
			if($field && $table_data['o'] == $field) {

				if($table_data['v'] == 'd') {
					$descending = 'descSelected';
					$ascending = 'asc';	
					$labelUrl = Router::GenerateUrl((isset($param['urlLabel'])?$param['urlLabel']:'smarty_generated_'.microtime()),array_merge($paramFromUrl, array('o' => $field,'v' => 'a')));
					$active = true;
				}else{
					$descending = 'desc';
					$ascending = 'ascSelected';
					$labelUrl = Router::GenerateUrl((isset($param['urlLabel'])?$param['laurlLabelbel']:'smarty_generated_'.microtime()),array_merge($paramFromUrl, array('o' => $field,'v' => 'd')));
					$active = true;
				}
			}else{
				$ascending = 'asc';
				$descending = 'desc';
				$labelUrl = Router::GenerateUrl((isset($param['urlLabel'])?$param['urlLabel']:'smarty_generated_'.microtime()),array_merge($paramFromUrl, array('o' => $field,'v' => 'a')));
				$active = false;
			}
			
			$smarty->assign('label',isset($param['label'])?$param['label']:'n/a');
			$smarty->assign('noSort',isset($param['noSort'])?$param['noSort']:'');
			$smarty->assign('ascending',$ascending);
			$smarty->assign('ascendingUrl',Router::GenerateUrl((isset($param['urlLabel'])?$param['urlLabel']:'smarty_generated_'.microtime()),array_merge($paramFromUrl, array('o' => $field,'v' => 'a'))));
			$smarty->assign('descending',$descending);
			$smarty->assign('descendingUrl',Router::GenerateUrl((isset($param['urlLabel'])?$param['urlLabel']:'smarty_generated_'.microtime()),array_merge($paramFromUrl, array('o' => $field,'v' => 'd'))));
			$smarty->assign('labelUrl',$labelUrl);
			$smarty->assign('active',$active);
			
			
			$return = $smarty->fetch($partial);
	}
	
	return $return;
}

?>